package com.softeem.xyblogmaster.controller;

import com.softeem.xyblogmaster.domain.Tags;
import com.softeem.xyblogmaster.service.CategoriesService;
import com.softeem.xyblogmaster.service.TagsService;
import com.softeem.xyblogmaster.util.AjaxResult;
import com.softeem.xyblogmaster.util.PageInfo;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Min;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/tags")
public class TagsController {

    //先注入Service
    @Autowired
    private TagsService tagsService;

    //增加方法 http://localhost:8080/tags/create
    @PostMapping("/create")
    public AjaxResult doAdd(@RequestBody @Valid Tags tags){
        tagsService.save(tags);
        return AjaxResult.ok("添加标签成功");
    }

    //http://localhost:8080/tags/edit
    @PutMapping("/edit")
    public AjaxResult  doEdit(@RequestBody @Valid Tags tags){
        tagsService.updateById(tags);
        return AjaxResult.ok("修改标签成功");
    }

    //http://localhost:8080/tags/remove/1
    @DeleteMapping("/remove/{id}")
    public AjaxResult doRemove(@PathVariable("id") int id){
        tagsService.removeById(id);
        return AjaxResult.ok("删除标签成功");
    }

    //http://localhost:8080/tags/detail/1
    @GetMapping("/detail/{id}")
    public AjaxResult doDetail(@PathVariable("id") @Min(1) int id){
        Tags byId = tagsService.getById(id);
        return AjaxResult.ok("查询标签成功",byId);

    }

//    //http://localhost:8080/list/1/10
//    @GetMapping("/list/{page}/{limit}")
//    public AjaxResult doList(@PathVariable("page") int page,
//                             @PathVariable("limit") int limit,
//                             @RequestParam("keyword") String keyword){
//
//        LambdaQueryWrapper<Articles> lambdaQueryWrapper=new LambdaQueryWrapper();
//        lambdaQueryWrapper.like(Articles::getTitle,"%"+keyword+"%");
//        Page pages = articlesService.page(new Page<>(page,limit),lambdaQueryWrapper);
//        PageInfo pageInfo = new PageInfo(pages,7);
//        return AjaxResult.ok("查询成功",pageInfo);
//
//    }

    //http://localhost:8080/tags?page=1&limit=10&keyword=java
    @GetMapping("/list")
    public AjaxResult doList(@RequestParam(value = "page",defaultValue = "1") int page,
                             @RequestParam(value = "limit",defaultValue = "10") int limit,
                             @RequestParam(value = "keyword",defaultValue = "") String keyword)

    {
        PageInfo pageInfo = tagsService.page(page, limit, keyword);
        return AjaxResult.ok("查询标签成功",pageInfo);

    }
}
